#include "const.h"

static void resetprojectconfig() {
	char projfile[512], *p;

	v_setvar("outfiles", "");
	v_setvar("infiles", "");
	v_setvar("indepfiles", "");

	v_setvar("srcdirs", "");
	v_setvar("incdirs", "");
	v_setvar("libdirs", "");

	v_setvar("srcfiles", "");
	v_setvar("objfiles", "");
	v_setvar("depfiles", "");

	v_setvar("projfile",
		i_realpath(v_getvar("projectfile"), projfile, sizeof(projfile)));
	if ((p=strrchr(projfile, '/')) != (char *)0)
		p[1] = 0;
	v_setvar("projroot", projfile);
}

int buildproject(var_t v, size_t i, void *p) {
	char toolchainfinal[512];
	int r;

	if (v||p||i) {/*unused*/}

	if (isverbose()) {
		fprintf(stdout, "building project \"%s\"\n", v_getvar("projectfile"));
		fflush(stdout);
	}

	resetprojectconfig();

	loadtoolchainconf("pre");

	if (!readconf(v_getvar("projectfile")))
		return 0;

	loadtoolchainconf("post");

	r = v_foreach(v_var("goals"), buildprojectgoal, (void *)0);

	v_nformat(toolchainfinal, sizeof(toolchainfinal),
		"$(toolchaindir)toolchain-final.conf");

	if (isfile(toolchainfinal))
		readconf(toolchainfinal);

	return r;
}
